package com.qingjiang.projectschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingjiang.projectschedule.mapper.StatisticMapper;
import com.qingjiang.projectschedule.pojo.Department;
import com.qingjiang.projectschedule.pojo.Statistic;
import com.qingjiang.projectschedule.pojo.StatisticChild;
import com.qingjiang.projectschedule.service.DepartmentService;
import com.qingjiang.projectschedule.service.ProjectScheduleService;
import com.qingjiang.projectschedule.service.StatisticChildService;
import com.qingjiang.projectschedule.service.StatisticService;
import com.qingjiang.projectschedule.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author jx
 * @description 针对表【statistic】的数据库操作Service实现
 * @createDate 2024-04-18 15:56:54
 */
@Service
public class StatisticServiceImpl extends ServiceImpl<StatisticMapper, Statistic>
        implements StatisticService {
    @Autowired
    private StatisticMapper statisticMapper;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ProjectScheduleService scheduleService;

    @Autowired
    private StatisticChildService statisticChildService;

    @Override
    public List<Map<String, Object>> getLastNodeAndState(String projectId, String accessMonth) {
        LambdaQueryWrapper<Statistic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Statistic::getLastNode, Statistic::getLastNodeState)
                .eq(Statistic::getProjectId, projectId)
                .eq(Statistic::getAccessTime, accessMonth);
        List<Map<String, Object>> maps = statisticMapper.selectMaps(queryWrapper);
        return maps;
    }

    @Override
    public void putCurMonthStatistic(String projectId, String projectName, Integer deptId, String curYearMonth, int curNodeId, int curNodeState, String yellowNodes, String redNodes) {
        LambdaQueryWrapper<Statistic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Statistic::getAccessTime, curYearMonth)
                .eq(Statistic::getProjectId, projectId);
        Statistic querySta = statisticMapper.selectOne(queryWrapper);
        if (querySta == null) {
            Statistic statistic = new Statistic();
            statistic.setAccessTime(curYearMonth);
            statistic.setProjectName(projectName);
            statistic.setDeptId(deptId);
            statistic.setLastNodeState(curNodeState);
            statistic.setLastNode(curNodeId);
            statistic.setProjectId(projectId);
            statistic.setYellowNode(yellowNodes);
            statistic.setRedNode(redNodes);
            statisticMapper.insert(statistic);
            return;
        }

        LambdaUpdateWrapper<Statistic> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Statistic::getLastNode, curNodeId)
                .set(Statistic::getLastNodeState, curNodeState)
                .set(Statistic::getYellowNode, yellowNodes)
                .set(Statistic::getRedNode, redNodes)
                .eq(Statistic::getProjectId, projectId)
                .eq(Statistic::getAccessTime, curYearMonth);
        statisticMapper.update(null, updateWrapper);
    }

    @Override
    public List<Map<String, Object>> getAllProjectByDateColorDeptId(Integer deptId, String curYearMonth, String color) {
        LambdaQueryWrapper<Statistic> queryWrapper = new LambdaQueryWrapper<>();
        if ("yellow".equals(color)) {
            queryWrapper.select(Statistic::getProjectId, Statistic::getProjectName, Statistic::getYellowNode);
        } else {
            queryWrapper.select(Statistic::getProjectId, Statistic::getProjectName, Statistic::getRedNode);
        }
        queryWrapper.eq(Statistic::getDeptId, deptId).eq(Statistic::getAccessTime, curYearMonth);
        //获得对应月份所有项目
        List<Map<String, Object>> maps = statisticMapper.selectMaps(queryWrapper);
        List<Map<String, Object>> res = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            //Map<String, Object> obj = new HashMap<>();
            String projectId = (String) map.get("project_id");
            //String projectName = (String) map.get("project_name");
            //map.put("project_name",projectName);
            String nodes = "yellow".equals(color) ? (String) map.get("yellow_node") : (String) map.get("red_node");
            int length = nodes.split(",").length;
            if (!nodes.isEmpty() && !" ".equals(nodes)) {
                map.put("node_name", "共" + length + "个");
                List<Map<String, Object>> children = scheduleService.getNodeByNodeIds(projectId, nodes);
                map.put("children", children);
                res.add(map);
            }
        }
        return res;
    }

    @Override
    public Result getHistoryProjectStatisticByYearAndMonth(String curYearMonth) {
        System.out.println("getHistoryProjectStatisticByYearAndMonth:" + curYearMonth);
        List<Department> deptList = departmentService.getDeptList();
        //定义结果集
        List<Map<String, Object>> list = new ArrayList<>();
        for (Department department : deptList) {
            Map<String, Object> eachDeptMap = new HashMap<>();
            LambdaQueryWrapper<Statistic> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(Statistic::getYellowNode, Statistic::getRedNode)
                    .eq(Statistic::getDeptId, department.getDeptId())
                    .eq(Statistic::getAccessTime, curYearMonth);
            List<Map<String, Object>> maps = statisticMapper.selectMaps(queryWrapper);
            Integer yellow_sum = 0;
            Integer red_sum = 0;
            int i = 0;
            for (Map<String, Object> map : maps) {
                String yellowNodes = (String) map.get("yellow_node");
                if (yellowNodes != null && !yellowNodes.isEmpty() && !" ".equals(yellowNodes)) {
                    yellow_sum += yellowNodes.split(",").length;
                }
                String redNodes = (String) map.get("red_node");
                if (redNodes != null && !redNodes.isEmpty() && !" ".equals(redNodes)) {
                    red_sum += redNodes.split(",").length;
                }
            }
            eachDeptMap.put("deptId", department.getDeptId());
            eachDeptMap.put("deptName", department.getDeptName());
            eachDeptMap.put("delayProjectSum", maps.size());
            eachDeptMap.put("yellowSum", yellow_sum);
            eachDeptMap.put("redSum", red_sum);

            StatisticChild statisticChild = statisticChildService.getHistoryInfoByYearAndMonth(department.getDeptId(), curYearMonth);
            if (statisticChild != null) {
                eachDeptMap.put("projectSum", statisticChild.getProjectSum());
                eachDeptMap.put("dPoints", statisticChild.getPoint());
            } else {
                eachDeptMap.put("projectSum", 0);
                eachDeptMap.put("dPoints", 0);
            }
            list.add(eachDeptMap);
        }
        return Result.ok(list);
    }

    @Override
    public void deleteCurrentMonthCancelProject(String projectId) {
        Calendar instance = Calendar.getInstance();
        int curYear = instance.get(Calendar.YEAR);
        int curMonth = instance.get(Calendar.MONTH) + 1;
        int curDay = instance.get(Calendar.DAY_OF_MONTH);
        String curYearMonth = String.valueOf(curYear) + String.valueOf(curMonth);
        if (curDay < 26) {
            LambdaQueryWrapper<Statistic> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Statistic::getProjectId, projectId).eq(Statistic::getAccessTime, curYearMonth);
            statisticMapper.delete(queryWrapper);
        }
    }
}




